<?php
declare (strict_types=1);

namespace app\srapi\controller;

use hg\apidoc\annotation as Apidoc;
use app\common\controller\SrapiController;
use phpcode\QrCode;
use think\facade\Db;

/**
 * @Apidoc\Title("首页")
 * @Apidoc\Group("Home")
 */
class Home extends SrapiController
{
    /**
     * @Apidoc\Title("轮播图")
     * @Apidoc\Method("GET")
     * @Apidoc\Tag("sr")
     * @Apidoc\Param("cate_id", desc="分类")
     * @Apidoc\Returned("title", type="int", desc="标题")
     * @Apidoc\Returned("image", type="int", desc="图")
     * @Apidoc\Returned("url", type="int", desc="链接")
     */
    public function banner()
    {
        $data = $this->request->param();
        $where = [];
        if (!empty($data['cate_id'])) {
            $where[] = ['cate_id', '=', $data['cate_id']];
        }
        $list = Db::name('system_banner')->where($where)->order('list desc')->select();
        $this->success('', $list);
    }

    /**
     * @Apidoc\Title("游戏")
     * @Apidoc\Method("GET")
     * @Apidoc\Tag("sr")
     * @Apidoc\Param("cate_id", desc="分类")
     * @Apidoc\Returned("title", type="int", desc="标题")
     * @Apidoc\Returned("image", type="int", desc="图")
     * @Apidoc\Returned("url", type="int", desc="链接")
     */
    public function game()
    {
        $data = $this->request->param();
        $where = [];
        if (!empty($data['cate_id'])) {
            $where[] = ['cate_id', '=', $data['cate_id']];
        }
        $list = Db::name('game')->where($where)->where("status=1 and delete_time is null")->order('id desc')->select();
        $this->success('', $list);
    }

    /**
     * @Apidoc\Title("商品列表")
     * @Apidoc\Method("GET")
     * @Apidoc\Tag("sr")
     * @Apidoc\Returned("title", type="int", desc="标题")
     * @Apidoc\Returned("image", type="int", desc="图")
     */
    public function goods_list()
    {
        $data = $this->request->param();
        $where = "status=1 and delete_time is null";
        if (!empty($data['keyword'])) {
            $title = $data['keyword'];
            $where .= " and title like '%$title%'";
        }

        $page = $data['page'] ?? 1;
        $limit = 10;
        $page = ($page - 1) * 10;
        $count = Db::name('goods')->where($where)->order('sort desc')->count();
        $list = Db::name('goods')->where($where)->order('sort desc')->limit($page, $limit)->select();
        $this->success('', ['data' => $list, 'count' => $count]);
    }

    /**
     * @Apidoc\Title("商品详情")
     * @Apidoc\Method("GET")
     * @Apidoc\Tag("sr")
     * @Apidoc\Returned("title", type="int", desc="标题")
     * @Apidoc\Returned("image", type="int", desc="图")
     */
    public function goods_info()
    {
        $data = $this->request->param();
        $where = "status=1 and delete_time is null";
        if (!empty($data['id'])) {
            $id = $data['id'];
            $where .= " and id = '$id'";
            Db::query("update sr_goods set count=count+1 where id=$id");
        }
        $list = Db::name('goods')->where($where)->find();
        $this->success('', $list);
    }


    public function info(){
        $data = $this->request->param();
        $find = Db::name('fabu')->where(['id'=>$data['id']])->find();
        $find['create_time'] = date('Y-m-d',(int)$find['create_time']);
        if (empty($find['qrcode'])) {
            $prefix = 'static/qrcode/';
            $id = $find['id'];

            $file_name = $prefix . 'mazhi_' . $id . '_0' . '.png';
            $result['file'] = 'http://' . $_SERVER['SERVER_NAME']  . '/static/qrcode/' . 'mazhi_' . $id . '_0' . '.png';

            $url = 'http://' . $_SERVER['SERVER_NAME'] . "/index.html#/pages/index/info?id=$id";
            $image = $result['file'];

            Db::query("update sr_fabu set qrcode='$image',mazhi='$url' where id=$id");

            if (!file_exists($file_name)) {
                QrCode::getQrCode('mazhi_' . $id . '_0', $id, $prefix, '10', 'L', $url);
            }
        }
        $this->success('', $find);
    }


    public function device_info(){
        $data = $this->request->param();
        $find = Db::name('device')->where(['title'=>$data['device'],'game_title'=>$data['game']])->find();
        $this->success('', $find);
    }


    public function login()
    {
        $data = $this->request->param();
        $where = "1";
        if (!empty($data['phone'])) {
            $phone = $data['phone'];
            $where .= " and phone = '$phone'";
        }
        if (!empty($data['pwd'])) {
            $pwd = $data['pwd'];
            $where .= " and password = '$pwd'";
        }
        $list = Db::name('member')->where($where)->find();
        $this->success('', $list);
    }

    public function register()
    {
        $data = $this->request->param();
        $where = "1";
        if (!empty($data['phone'])) {
            $phone = $data['phone'];
            $pwd = $data['pwd'];

            $where .= " and phone = '$phone'";
            $ishave = Db::name('member')->where($where)->find();
            if ($ishave) {
                $this->success('账号已注册', '');
                exit;
            } else {
                Db::name("member")->insert(['phone' => $phone, 'password' => $pwd,'create_at'=>date('Y-m-d H:i:s')]);
                $list = Db::name('member')->where($where)->find();
                $this->success('', $list);
            }
        } else {
            $this->success('系统错误', '');
        }
    }

    public function member()
    {
        $data = $this->request->param();
        $where = "1";
        if (!empty($data['phone'])) {
            $phone = $data['phone'];
            $where .= " and phone = '$phone'";
            $list = Db::name('member')->where($where)->find();
            $this->success('', $list);
        }
    }

    public function edit_password()
    {
        $data = $this->request->param();
        $where = "1";
        if (!empty($data['phone'])) {
            $phone = $data['phone'];
            $oldpwd = $data['oldpwd'];

            $where .= " and phone = '$phone' and password='$oldpwd'";
            $ishave = Db::name('member')->where($where)->find();
            if (empty($ishave)) {
                $this->success('旧密码错误', '');
                exit;
            } else {
                $pwd = $data['pwd'];
                Db::name('member')->where($where)->update(['password' => $pwd]);

                $this->success('', Db::name('member')->where("phone=$phone")->find());
            }
        } else {
            $this->success('系统错误', '');
            exit;
        }
    }

    public function kefu()
    {
        $this->success('', Db::name('kefu')->where("id=10")->find());
    }

    public function forget_password()
    {
        $data = $this->request->param();
        $where = "1";
        if (!empty($data['phone'])) {
            $phone = $data['phone'];

            $where .= " and phone = '$phone'";
            $ishave = Db::name('member')->where($where)->find();
            if (empty($ishave)) {
                $this->success('账号未找到', '');
                exit;
            }

            $pwd = $data['pwd'];
            Db::name('member')->where($where)->update(['password' => $pwd]);

            $this->success('', Db::name('member')->where("phone=$phone")->find());
        } else {
            $this->success('系统错误', '');
            exit;
        }
    }

    //获取后缀
    public function getSuffix($picname){
        $pos = strrpos($picname,'.');
        $suffix = substr($picname,$pos+1);
        return $suffix;
    }

    public function uploadImages(){
        if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['file'])) {
            $file = $_FILES['file'];
            $getSuffix = $this->getSuffix($file['name']);
            $targetDir = "uploads/"; // 文件存储目录，确保目录存在且可写
            $targetFile = $targetDir . basename(md5($file['name'].time())).'.'.$getSuffix; // 目标文件路径和名称
            if (move_uploaded_file($file['tmp_name'], $targetFile)) {
                echo $targetFile; // 返回成功消息给客户端
            } else {
                echo "文件上传失败"; // 返回错误消息给客户端
            }
        } else {
            echo "无效的请求"; // 非POST请求或没有文件字段的情况返回错误消息给客户端
        }
    }

    public function cj_count()
    {
        $cj_count = Db::name('system_config')->where("name='cj_count'")->value('value');
        $this->success('', $cj_count);
        exit;
    }

    public function advice_img()
    {
        $advice_img = Db::name('system_config')->where("name='advice_img'")->value('value');
        $this->success('', $advice_img);
        exit;
    }

    public function ad_text()
    {
        $ad_text = Db::name('system_config')->where("name='ad_text'")->value('value');
        $this->success('', $ad_text);
        exit;
    }

    public function updateTitle(){
        $data = $this->request->param();
        $title = $data['title'];
        Db::name('member')->where(['id'=>$data['id']])->update(['title'=>$title]);
        $this->success('', '');
        exit;
    }

    public function updateImgOrNick(){
        $data = $this->request->param();
        $image = $data['image'];
        Db::name('member')->where(['id'=>$data['id']])->update(['image'=>$image]);
        $this->success('', '');
        exit;
    }

    public function game_select()
    {
        $where = "status=1 and delete_time is null";
        $data = Db::name('game')->where($where)->select();
        $this->success('', $data);
        exit;
    }

    public function game_zhiye()
    {
        $where = "status=1 and delete_time is null";
        $data = $this->request->param();
        if (!empty($data['game'])) {
            if($data['game']!='未选择'){
                $game = $data['game'];
                $where .= " and title='$game'";
            }
            
        }
        $find = Db::name('game')->where($where)->find();
        if($find){
            $zhiye = explode(',',$find['remark']);
            $arr = ['请选择职业'];
            foreach ($zhiye as $k =>&$v){
                array_push($arr,$v);
            }
            $this->success('', $arr);
            exit;
        }
    }

    public function device_select()
    {
        $where = "status=1 and delete_time is null";
        $data = $this->request->param();
        if (!empty($data['game'])) {
            $game = $data['game'];
            $where .= " and game_title='$game'";
        }
        $data = Db::name('device')->where($where)->select();
        $this->success('', $data);
        exit;
    }

    public function fabu(){
        $data = $this->request->param();
        $data['create_time'] = time();
        Db::name('fabu')->insert($data);
        $this->success('', $data);
        exit;
    }

    public function save_fabu(){
        $data = $this->request->param();
        $data['update_time'] = time();
        $data['is_shenhe'] = 1;
        $data['shenhe_reason'] = '';
        Db::name('fabu')->where(['id'=>$data['id']])->update($data);
        $this->success('', $data);
        exit;
    }

    public function del()
    {
        $data = $this->request->param();
        Db::name('fabu')->where(['id'=>$data['id']])->delete();
        $this->success('', $data);
        exit;
    }

    public function collect()
    {
        $data = $this->request->param();
        $ishave = Db::name("collect")->where(['fabu_id'=>$data['fabu_id'],'member_id'=>$data['member_id']])->find();
        if(empty($ishave)){
            $data['create_at'] = date('Y-m-d H:i:s');
            Db::name("collect")->insert($data);
            $this->success('', $data);
            exit;
        }else{
            $this->success('您已收藏', $data);
            exit;
        }
    }

    public function my_collect()
    {
        $data = $this->request->param();
        $select = Db::name('collect')->where(["member_id"=>$data['member_id']])->select();
        $arr = [];
        foreach ($select as $k =>&$v){
            array_push($arr,$v['fabu_id']);
        }
        if(count($arr)>0){
            $ids = implode(',',$arr);
            $list = Db::name('fabu')->where("id","in",$ids)->select();
        }else{
            $list = [];
        }
        $arr = [];
        foreach ($list as $k =>&$v){
            $v['create_time'] = date('Y-m-d',(int)$v['create_time']);
            array_push($arr,$v);
        }
        $this->success('', $arr);
        exit;
    }

    public function my_fabu()
    {
        $data = $this->request->param();
        $where = "1";
        if (!empty($data['member_phone'])) {
            $member_phone = $data['member_phone'];
            $where .= " and member_phone='$member_phone'";
            $tab = $data['tab'];
            if($tab==1){
                $where .= "and is_shenhe=1";
            }
            if($tab==2){
                $where .= "and is_shenhe=2";
            }
            if($tab==3){
                $where .= "and is_shenhe=0";
            }


            $list = Db::name('fabu')->where($where)->order("id desc")->select();
            $arr = [];
            foreach ($list as $k =>&$v){
                $v['create_time'] = date('Y-m-d',(int)$v['create_time']);
                array_push($arr,$v);
            }
            $this->success('', $arr);
            exit;
        }else{
            $this->success('', []);
            exit;
        }
    }

    public function cancel_collect(){
        $data = $this->request->param();
        Db::name('collect')->where(['fabu_id'=>$data['fabu_id'],'member_id'=>$data['member_id']])->delete();
        $this->success('', []);
        exit;
    }

    public function account_list(){
        $data = $this->request->param();
        $where = "status=1 and delete_time is null and is_shenhe=2";

        if($data['game']=='未选择'){$data['game'] = '';}
        if(!empty($data['game'])){
            $game = $data['game'];
            $where .= " and game='$game'";
        }

        if($data['device']=='未选择'){$data['device'] = ''; }
        if(!empty($data['device'])){
            $device = $data['device'];
            $where .= " and device='$device'";
        }

        if($data['zhiye_name']=='未选择'){$data['zhiye_name'] = ''; }
        if(!empty($data['zhiye_name'])){
            $zhiye_name = $data['zhiye_name'];
            $where .= " and person_zhiye='$zhiye_name'";
        }

        if($data['max_level']){
            $max_level = $data['max_level'];
            $where .= " and person_level<=$max_level";
        }

        if($data['min_level']){
            $min_level = $data['min_level'];
            $where .= " and person_level>=$min_level";
        }

        $order = "id desc";
        if($data['sort']==1){
            $order = "person_level desc";
        }
        if($data['sort']==2){
            $order = "id desc";
        }
        if($data['sort']==3){
            $order = "price desc";
        }
        if($data['sort']==4){
            $order = "hot desc";
        }

        $page = $data['page']??1;
        $page = ($page - 1)*10;

        $list = Db::name('fabu')->where($where)->order($order)->limit($page,10)->select();
        $arr = [];
        foreach ($list as $k =>&$v){
            $v['create_time'] = date('Y-m-d',(int)$v['create_time']);
            array_push($arr,$v);
        }
        $this->success('', $arr);
        exit;
    }

    /**
     * @Apidoc\Title("是否交付")
     * @Apidoc\Method("GET")
     * @Apidoc\Tag("sr")
     * @Apidoc\Returned("state", type="int", desc="1未交付2已交付")
     * @Apidoc\Returned("image", type="int", desc="提示图片")
     */
    public function check()
    {
        $this->success('', ['state' => 1, 'image' => 'https://api.rjaaa.com/common/images/tc_add_wap.png']);
    }
}